package org.sundy.net;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class Demo {

	public static void main(String[] args) throws Exception {
		URL url = new URL("http://localhost:8080/api-service-json/fetchData");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		conn.setRequestMethod("POST");
		String params = "{CLWID:12,roadCode:'D011',keyword:'',pageIndex:1,pageSize:10}";
		conn.setUseCaches(false);
		conn.setRequestProperty("Content-Type", "application/octet-stream");
		conn.setRequestProperty("Content-Length",
				String.valueOf(params.length()));
		conn.setRequestProperty("pure-data", "yes");
		conn.setRequestProperty("Connection", "Keep-Alive");
		conn.setDoInput(true);
		conn.connect();
		PrintWriter out = new PrintWriter(conn.getOutputStream());

		out.println(new String(params.getBytes("UTF-8")));
		out.flush();
		out.close();
		int resCode = conn.getResponseCode();
		if (200 == resCode) {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream(), "UTF-8"));
			String line = "";
			StringBuffer sb = new StringBuffer();
			while (null != (line = reader.readLine())) {
				sb.append(line);
			}
			System.out.println(sb.toString());
		}
		System.out.println(resCode);
		conn.disconnect();
	}
}
